package gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import plugin.redesign.RedesignHandler;

import writer.ASLFileWriter;

import java.awt.TextArea;
import java.awt.event.MouseEvent;
import java.io.File;

public class ConfirmWindow extends JDialog {

    private final JPanel contentPanel = new JPanel();
    private final TextArea textArea = new TextArea();

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            ConfirmWindow dialog = new ConfirmWindow();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the dialog.
     */
    public ConfirmWindow() {
        setTitle("Edit and Confirm");
        setBounds(100, 100, 450, 250);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setLayout(new FlowLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        {
            contentPanel.add(textArea);
        }
        {
            JPanel buttonPane = new JPanel();
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPane, BorderLayout.SOUTH);
            {
                JButton okButton = new JButton("OK");
                okButton.setActionCommand("OK");
                buttonPane.add(okButton);
                getRootPane().setDefaultButton(okButton);
                
                okButton.addMouseListener(new java.awt.event.MouseAdapter() {
                    public void mouseClicked(java.awt.event.MouseEvent evt) {
                        okButtonClicked(evt);
                    }
                });
            }
            {
                JButton cancelButton = new JButton("Cancel");
                cancelButton.setActionCommand("Cancel");
                buttonPane.add(cancelButton);
                cancelButton.addMouseListener(new java.awt.event.MouseAdapter() {
                    public void mouseClicked(java.awt.event.MouseEvent evt) {
                        cancelButtonClicked(evt);
                    }
                });
            }
        }
    }
    
    protected void cancelButtonClicked(MouseEvent evt) {
        this.dispose();
        
    }

    protected void okButtonClicked(MouseEvent evt) {
        ASLFileWriter.writeASLFile(RedesignHandler.projectPath + File.separator + "src"+ File.separator + "redesign_"
              + RedesignHandler.fileName, textArea.getText());
        this.dispose();
        RedesignHandler.generateSuggestion(RedesignHandler.projectPath + File.separator + "src"+ File.separator + "redesign_"
        + RedesignHandler.fileName);
    }

    public TextArea getTextArea() {
        return textArea;
    }

}
